<?php

class Cab_Action_Helper_HelperAuth extends Zend_Controller_Action_Helper_Abstract {

    protected $_user;
    protected $_username;

    public function preDispatch() {

        $controller = $this->getActionController()->getRequest()->getControllerName();
        //echo "<h2>" . var_dump($this->getActionController()->getRequest()->getControllerName()) . "</h2>";

        //echo "<h2>USER : " . $this->_user . "</h2>";

        if ($this->_user == null /* && $controller != 'login' */) {
            // echo "<h2>" . $controller . "</h2>";
            $auth = Zend_Auth::getInstance();

            if ($auth->hasIdentity()) {
                $user = $auth->getIdentity();
                //echo "<h2>" . $user->mail . "</h2>";
                // var_dump($user);

                $this->_username = ucwords($user->prenom . " " . $user->nom);
                $this->_user = $user;

                Zend_Registry::set('user', $this->_user);
            } else {
                $this->_user = false;
                Zend_Registry::set('user', false);

                //$this->_helper->Redirector('login');
                /*
                  $request = $this->getRequest();
                  $request->setControllerName('login')
                  ->setActionName('index')
                  ->setDispatched(false);
                  echo "<h2>REDIRECTION</h2>";
                 */
            }
        }

        if ($this->_user instanceof User) {
            echo "<h2>instanceof</h2>";
            $this->getActionController()->setUser($this->_user);
        }
    }

}

